package com.intellagric.service.impl.module.sys_user;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysUserMapper;
import com.intellagric.mapper.SysUserRoleMapper;
import com.intellagric.pojo.SysUser;
import com.intellagric.pojo.SysUserExample;
import com.intellagric.pojo.SysUserRoleExample;
import com.intellagric.pojo.SysUserRoleKey;
import com.intellagric.service.module.sys_user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2018/10/4
 * @Description: 用户管理服务
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 添加用户和角色信息
     * @Param sysUser
     * @return LayuiDataGridResult
    */
    @Override
    public ResponseMessage addUser(SysUser sysUser) {
        SysUserRoleKey sysUserRoleKey = new SysUserRoleKey();
        sysUserRoleKey.setUserId(sysUser.getId());
        sysUserRoleKey.setRoleId(sysUser.getRoleId());
        if (sysUserMapper.insertSelective(sysUser) == 1 &&
                sysUserRoleMapper.insertSelective(sysUserRoleKey) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除用户
     * @Param id
     * @return LayuiDataGridResult
     */
    @Override
    public ResponseMessage deleteUser(String id) {
        if (sysUserMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        }else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改用户信息和角色信息
     * @Param sysUser
     * @return LayuiDataGridResult
     */
    @Override
    public ResponseMessage editUser(SysUser sysUser) {
        //修改角色信息
        SysUserRoleKey sysUserRoleKey = new SysUserRoleKey();
        sysUserRoleKey.setUserId(sysUser.getId());
        sysUserRoleKey.setRoleId(sysUser.getRoleId());
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(sysUser.getId());
        //修改
        if (sysUserMapper.updateByPrimaryKeySelective(sysUser) == 1 &&
                sysUserRoleMapper.updateByExample(sysUserRoleKey,sysUserRoleExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询用户信息
     * @Param id
     * @return LayuiDataGridResult
     */
    @Override
    public SysUser getUserById(String id) {
        //可先查询缓存，再到数据库查询，待定
        //查询用户角色信息
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(id);
        List<SysUserRoleKey> userRoles = sysUserRoleMapper.selectByExample(sysUserRoleExample);
        //查询用户信息
        SysUser user = sysUserMapper.selectByPrimaryKey(id);
        if (userRoles.size() != 0) {
            user.setRoleId(userRoles.get(0).getRoleId());
        }
        return user;
    }

    /**
     * 查询用户列表信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getUserList() {
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.createCriteria().andIdIsNotNull();
        List<SysUser> userList = sysUserMapper.selectByExample(sysUserExample);
        return LayuiDataGridResult.success().add(userList,userList.size());
    }

    /**
     * 分页查询用户列表信息
     * @Param page
     * @Param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getUserList(int page, int rows) {
        PageHelper.startPage(page, rows);
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.createCriteria().andIdIsNotNull();
        List<SysUser> users = sysUserMapper.selectByExample(sysUserExample);
        return LayuiDataGridResult.success().add(users,users.size());
    }

}
